﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using EzDesk.Clients.Utility;
using EzDesk.Utility.Web.Controllers;
using EzDesk.Core.Model;
using EzDesk.Clients.MobileWeb.CoreClientService;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Net;

namespace EzDesk.Clients.MobileWeb.Controllers
{
    [HandleError]
    public class HomeController : EzDeskMobileClientController
    {
        [Authorize]
        public ActionResult Index()
        {
            // 获得最新的几条Notifications
            var notisData = CoreClientService.GetXmlFeedsByCount("notification", CurrentUserId, 0, 5, null, true, null);

            // 获取第一页Update
            var updatesData = CoreClientService.GetXmlFeedsByCount("update", CurrentUserId, 0, 20, null, true, null);

            FeedCollection notifications = transformXmlToHtml(notisData);
            FeedCollection updates = transformXmlToHtml(updatesData);

            var allAppFunctions = CoreClientService.GetAllAppFunctions(CurrentUserId);
            IList<FunctionType> functionTypeList = new List<FunctionType>();
            foreach (var appFunction in allAppFunctions)
            {
                if (!functionTypeList.Contains(appFunction.Type))
                {
                    functionTypeList.Add(appFunction.Type);
                }
            }

            ViewData["notifications"] = notifications;
            ViewData["updates"] = updates;

            ViewData["currentUser"] = CoreClientService.GetSimpleUserById(CurrentUserId);

            return View();
        }

        [Authorize]
        public ActionResult AppFunction(string appFunctionTypeName)
        {
            var appFunctions = CoreClientService.GetAppFunctionsByTypeName(CurrentUserId, appFunctionTypeName);
            if (appFunctions != null && appFunctions.Count != 0)
            {
                // 给AppFunctions按照Category分组
                var categories =
                    appFunctions
                        .OrderBy(p => p.Privilege)
                        .GroupBy(p => p.Category.DisplayText)
                        .Select(p => new { DisplayText = p.Key });

                var groupedAppFunctions = new Dictionary<string, IList<AppFunction>>();
                foreach (var category in categories)
                {
                    groupedAppFunctions.Add(
                        category.DisplayText,
                        appFunctions
                            .Where(p => p.Category.DisplayText == category.DisplayText)
                            .OrderBy(p => p.Privilege)
                            .ToList()
                        );
                }

                // 一级分类名称
                ViewData["typeDisplayText"] = appFunctions.FirstOrDefault().Type.DisplayText;
                ViewData["appFunctions"] = groupedAppFunctions;
            }
            else
            {
                ViewData["NoData"] = true;
            }

            return View();
        }

        private FeedCollection transformXmlToHtml(string xmlData)
        {
            var items = FeedCollection.Create(xmlData);
            return items;
        }
    }
}
